﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using Common;
using Common.Config;
using Common.Model;
using ConsoleService.API;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Web;
using Quartz;

namespace ConsoleService.Job
{
    /// <summary>
    /// 创建IJob的实现类，并实现Excute方法
    /// </summary>
    public class MyJob2 : IJob
    {
        Logger logger = LogManager.GetCurrentClassLogger();
        public MyJob2()
        {
            NLog.Web.NLogBuilder.ConfigureNLog("nlog.config");
        }

        public Task Execute(IJobExecutionContext context)
        {
            //var triggerData = context.Trigger.JobDataMap;//获取Trigger中的参数
            //var data = context.MergedJobDataMap;//获取Job和Trigger中合并的参数
            //var value1 = triggerData.GetInt("key1");
            //var value2 = triggerData.GetString("key2");

            return Task.Run(() =>
            {

                Stopwatch s = new Stopwatch();
                s.Start();
                UserAPI api = new UserAPI();
                var currentUser = api.GetUserToken();

                RequestModel request = new RequestModel()
                {
                    Method = RestSharp.Method.GET,
                    Token = currentUser.data.Token,
                    Resources = "/api/Common/InitCity",
                    WebSide = Configs.WebUrl ,
                };

                var result = WebHelper.HttpRequest(request);
                var msg = $"同步redis数据成功用时：{ s.Elapsed.ToString()}" + DateTime.Now.ToString();
                s.Stop();
                api.WrittenQueue(msg, currentUser.data.Token);
                Console.WriteLine(msg);
            });
        }
    }
}
